package com.ukao.cashregister.ui.sendFactory;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.bean.FactoryBatchBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.widget.ClearEditText;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 送厂
 */
public class FactoryTabFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.batch_entry_btn)
    Button batchEntryBtn;
    @BindView(R.id.m_title)
    TextView mTitle;
    @BindView(R.id.all_batch)
    RadioButton allBatch;
    @BindView(R.id.receive_batch)
    RadioButton receiveBatch;
    @BindView(R.id.midway_batch)
    RadioButton midwayBatch;
    @BindView(R.id.work_factory)
    RadioButton workFactory;
    @BindView(R.id.segmented_group)
    SegmentedGroup segmentedGroup;
    @BindView(R.id.franment_layout)
    FrameLayout franmentLayout;
    @BindView(R.id.fragment_factory_search1_edit)
    ClearEditText mTextInput;
    @BindView(R.id.data_search_btn)
    Button searchBtn;


    private String mParam1;
    private String mParam2;

    private FactoryBatchFragment mFragment1;
    private FactoryBatchFragment mFragment2;
    private FactoryBatchFragment mFragment3;
    private FactoryBatchFragment mFragment4;
    private Unbinder unbinder;

    public static FactoryTabFragment newInstance(String param1, String param2) {
        FactoryTabFragment fragment = new FactoryTabFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_factory_batch, container, false);
        unbinder= ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }


    @Override
    protected void initListener() {
        super.initListener();
        segmentedGroup.setOnCheckedChangeListener(this);
        //设置文本输入框的监听事件
        mTextInput.setOnEditorActionListener(mOnEditorAction);
        mTextInput.requestFocus();
    }


    @Override
    protected void initView() {
        FactoryBatchFragment firstFragment = findFragment(FactoryBatchFragment.class);
        if (firstFragment == null) {
            mFragment1 = FactoryBatchFragment.newInstance("", "");
            mFragment2 = FactoryBatchFragment.newInstance("1", "");
            mFragment3 = FactoryBatchFragment.newInstance("2", "");
            mFragment4 = FactoryBatchFragment.newInstance("3", "");
            allBatch.setChecked(true);
            loadMultipleRootFragment(R.id.franment_layout, 0,
                    mFragment1,
                    mFragment2,
                    mFragment3,
                    mFragment4);
        } else {
            mFragment1 = firstFragment;
            mFragment2 = findFragment(FactoryBatchFragment.class);
            mFragment3 = findFragment(FactoryBatchFragment.class);
            mFragment4 = findFragment(FactoryBatchFragment.class);
        }

    }

    TextView.OnEditorActionListener mOnEditorAction = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (!CheckUtils.isNull(event) && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) {
                    String BagCode   =getText(mTextInput);
                if(!TextUtils.isEmpty(BagCode)){
                    searchBagNumber(BagCode);
                }else {
                    T.show("请输入正确的袋号");
                }
            }
            return false;
        }
    };
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.batch_entry_btn,R.id.data_search_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.batch_entry_btn:
                batchInadd("");
                break;
            case R.id.data_search_btn:   //搜索袋号
                String BagCode   =getText(mTextInput);
                if(!TextUtils.isEmpty(BagCode)){
                    searchBagNumber(BagCode);
                }else {
                    T.show("请输入正确的袋号");
                }
                hideSoftInput();  //隐藏软键盘
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.all_batch:
                showHideFragment(mFragment1);
                break;
            case R.id.receive_batch:
                showHideFragment(mFragment2);
                break;
            case R.id.midway_batch:
                showHideFragment(mFragment3);
                break;
            case R.id.work_factory:
                showHideFragment(mFragment4);
                Log.d("触发了", "onCheckedChanged: ");
                break;
            default:

        }
    }

    /**
     *  搜索袋号
     */
    public  void searchBagNumber(String BagCode){
        mTextInput.getText().clear();//设置为""
        if(mFragment1.isSupportVisible()){
            mFragment1.requestSearchBagNumber(BagCode);
        }else if(mFragment2.isSupportVisible()){
            mFragment2.requestSearchBagNumber(BagCode);
        }else if(mFragment3.isSupportVisible()){
            mFragment3.requestSearchBagNumber(BagCode);
        }else if(mFragment4.isSupportVisible()){
            mFragment4.requestSearchBagNumber(BagCode);
        }
    }

    public void batchInadd(String productIds) {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderProIds", productIds);
        mHashMap.put("accessToken", SaveParamets.getToken(_mActivity));
        addSubscription(apiStores().batchInadd(Constant.createParameter(mHashMap)),
                new ApiCallback<FactoryBatchBean>() {
                    @Override
                    public void onSuccess(FactoryBatchBean model) {
                        if (model.getHttpCode() == 200) {
                            FactoryBatchBean mDataBean = model.getData();
                            start(BatchDetailsFragment.newInstance(mDataBean.getId(), mDataBean.getIsFinish(), mDataBean.getProductCnt()));


                        } else {
                            T.show(model.getMsg());

                        }


                    }


                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }

                });
    }
}
